package com.woniuxy.controller;

import com.sun.org.apache.xpath.internal.operations.Mod;
import com.woniuxy.entity.RbacManager;
import com.woniuxy.service.RbacManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Author: 马宇航
 * @Todo: 用户管理的Controller控制层
 * @DateTime: 22/08/24/0024 14:17
 * @Component: 成都蜗牛学苑
 **/
// 相当于 @Service 注册到Spring容器中
@Controller
// 相当于<bean id = ”/rbacManager“ class="..">
// http://localhost:8080/rbacManager  这个请求就能够找到我的这个Controller！！！
@RequestMapping("/rbacManager")
public class RbacManagerController {
    @Autowired
    RbacManagerService rbacManagerService;
    /**
     * 查询所有用户
     * ChangeLog : 1. 创建 (22/08/24/0024 14:23 [马宇航]);
     *  @RequestMapping("/findAll") 相当于，url请求：http://localhost:8080/rbacManager/findAll
     * @return java.lang.String
    */
    @RequestMapping("/findAll")
    public String findAll(Model model, ModelMap modelMap, HttpServletRequest request, HttpServletResponse response, HttpSession httpSession) throws ServletException, IOException {
        List<RbacManager> managers = rbacManagerService.findAll();
        model.addAttribute("data",managers);
//        request.setAttribute("data",managers);
//        request.getRequestDispatcher("/WEB-INF/jsp/success.jsp").forward(request,response);
        return "forward:/findByName?name=a";
    }
    @RequestMapping("/findByName") //http://localhost:8080/rbacManager/findByName？name=sfs
    public String findByName(@RequestParam("name") String username, Model model){
        List<RbacManager> list = rbacManagerService.findByName(username);
        model.addAttribute("data",list);
        return "success";
    }
    //限制请求为POST请求 @PostMapping 也行！
    @RequestMapping(value = "/findByIds",method = RequestMethod.POST)
    public String findByIds(Integer[] ids, Model model){
        List<RbacManager> list = rbacManagerService.findByIds(ids);
        model.addAttribute("data",list);
        return "success";
    }
    @RequestMapping(value = "/findByLists",method = RequestMethod.POST)
    public String findByLists(@RequestParam List<Integer> ids, Model model){
        Integer[] objects = ids.toArray(new Integer[0]);
        List<RbacManager> list = rbacManagerService.findByIds(objects);
        model.addAttribute("data",list);
        return "success";
    }
    @RequestMapping(value = "/addOneForMap")
    public String addOneForMap(@RequestParam Map<String,Object> userinfo,Model model){
        if (userinfo.size()!=0) {
            int i = rbacManagerService.addOneForMap(userinfo);
        }
        model.addAttribute("data","新增用户："+userinfo);
        return "success";
    }
    @RequestMapping("/addOne")
    public String addOne(RbacManager rbacManager,Model model){
        int i = rbacManagerService.addOne(rbacManager);
        model.addAttribute("data","新增用户："+rbacManager);
        return "success";
    }
}
